<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="applNm">
        <el-input
          v-model="queryParams.applNm"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名日期">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy.MM.dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <!-- <el-form-item label="学历" prop="eduType">
        <el-select
          v-model="queryParams.eduType"
          placeholder="请选择学历"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in eduTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="作业种类" prop="operType">
        <el-select
          v-model="queryParams.operType"
          placeholder="请选择作业种类"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in operTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="作业项目" prop="operItemsType">
        <el-select
          v-model="queryParams.operItemsType"
          placeholder="请选择作业项目"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in operItemsTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单位" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="identity">
        <el-input
          v-model="queryParams.identity"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          style="width: 130px"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="缴费情况" prop="paymentQryList">
        <el-select
          v-model="queryParams.paymentQryList"
          placeholder="请输入缴费情况"
          multiple
          filterable
          allow-create
          clearable
          default-first-option
          size="small"
        >
          <el-option
            v-for="dict in paymentTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          >
          </el-option>
        </el-select>
        <!-- <el-input
          v-model="queryParams.payment"
          placeholder="请输入缴费情况"
          clearable
          size="small"
          style="width: 130px"
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['busi:appl:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['busi:appl:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['busi:appl:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['busi:appl:import']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['busi:appl:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="applList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="30" align="center" fixed />
      <el-table-column
        label="报名日期"
        align="center"
        prop="applTime"
        width="90"
        fixed
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applTime, "{y}.{m}.{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column type="index" width="40" align="center" fixed>
      </el-table-column>
      <!-- <el-table-column label="ID" align="center" prop="applId" fixed /> -->
      <el-table-column
        label="姓名"
        width="70"
        align="center"
        prop="applNm"
        fixed
      />
      <el-table-column
        label="性别"
        align="center"
        prop="sex"
        :formatter="sexFormat"
      />
      <el-table-column
        label="身份证号"
        width="200"
        align="center"
        prop="identity"
      />
      <el-table-column
        label="学历"
        align="center"
        prop="eduType"
        :formatter="eduTypeFormat"
      />
      <el-table-column
        label="作业种类"
        width="190"
        align="center"
        prop="operType"
        :formatter="operTypeFormat"
      />
      <el-table-column
        label="作业项目"
        width="200"
        align="center"
        prop="operItemsType"
        :formatter="operItemsTypeFormat"
      />
      <el-table-column label="单位" width="200" align="center" prop="company" />
      <el-table-column label="地址" width="200" align="center" prop="addr" />
      <el-table-column
        label="联系电话1"
        width="110"
        align="center"
        prop="phone"
      />
      <el-table-column
        label="联系电话2"
        width="110"
        align="center"
        prop="tel"
      />
      <el-table-column
        label="缴费情况"
        width="110"
        align="center"
        prop="payment"
      />
      <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column
        label="负责人电话"
        width="110"
        align="center"
        prop="leaderPhone"
      />
      <el-table-column
        label="通知情况"
        width="300"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        width="120"
        class-name="small-padding fixed-width"
         v-if="checkPermi(['busi:appl:edit', 'busi:appl:remove'])"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['busi:appl:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['busi:appl:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报名管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="applNm">
          <el-input v-model="form.applNm" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="identity">
          <el-input
            v-model="form.identity"
            placeholder="请输入身份证号"
            maxlength="18"
          />
        </el-form-item>
        <el-form-item label="报名日期" prop="applTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.applTime"
            type="date"
            value-format="yyyy.MM.dd"
            placeholder="选择报名日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学历" prop="eduType">
          <el-select v-model="form.eduType" placeholder="请选择学历">
            <el-option
              v-for="dict in eduTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作业种类" prop="operType">
          <el-select v-model="form.operType" placeholder="请选择作业种类">
            <el-option
              v-for="dict in operTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作业项目" prop="operItemsType">
          <el-select v-model="form.operItemsType" placeholder="请选择作业项目">
            <el-option
              v-for="dict in operItemsTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="理论成绩" prop="theoryScores">
          <el-input v-model="form.theoryScores" placeholder="请输入理论成绩" />
        </el-form-item>
        <el-form-item label="实操成绩" prop="practiceScores">
          <el-input
            v-model="form.practiceScores"
            placeholder="请输入实操成绩"
          />
        </el-form-item>
        <el-form-item label="单位" prop="company">
          <el-input v-model="form.company" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入联系电话1"
            maxlength="11"
          />
        </el-form-item>
        <el-form-item label="联系电话2" prop="tel">
          <el-input
            v-model="form.tel"
            placeholder="请输入联系电话2"
            maxlength="11"
          />
        </el-form-item>
        <el-form-item label="缴费情况" prop="payment">
          <el-input v-model="form.payment" placeholder="请输入缴费情况" />
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人联系电话" prop="leaderPhone">
          <el-input
            v-model="form.leaderPhone"
            placeholder="请输入负责人联系电话"
            maxlength="11"
          />
        </el-form-item>
        <el-form-item label="通知情况" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 报名管理导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox
            v-model="upload.updateSupport"
          />是否更新已经存在的报名数据
          <el-link type="info" style="font-size:12px" @click="importTemplate"
            >下载模板</el-link
          >
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAppl,
  getAppl,
  delAppl,
  addAppl,
  updateAppl,
  exportAppl,
  importTemplate
} from "@/api/busi/appl";
import { getToken } from "@/utils/auth";
import loginVue from "../../login.vue";
import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
export default {
  name: "Appl",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报名管理表格数据
      applList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 性别字典
      sexOptions: [],
      // 日期范围
      dateRange: [],
      // 学历字典
      eduTypeOptions: [],
      // 作业种类字典
      operTypeOptions: [],
      // 作业项目字典
      operItemsTypeOptions: [],
      // 缴费情况字典
      paymentTypeOptions: [],
      // 报名导入参数
      upload: {
        // 是否显示弹出层（报名导入）
        open: false,
        // 弹出层标题（报名导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的报名数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/busi/appl/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applNm: null,
        sex: null,
        identity: null,
        applTime: null,
        eduType: null,
        operType: null,
        operItemsType: null,
        company: null,
        phone: null,
        payment: null,
        paymentQryList: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        applNm: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
          { min: 1, max: 15, message: "长度在 1 到 15 个字符", trigger: "blur" }
        ],
        sex: [{ required: true, message: "性别不能为空", trigger: "change" }],
        identity: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
          {
            pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
            message: "请输入正确的身份证号",
            trigger: "blur"
          }
        ],
        applTime: [
          { required: true, message: "报名日期不能为空", trigger: "blur" }
        ],
        eduType: [
          { required: true, message: "学历不能为空", trigger: "change" }
        ],
        operType: [
          { required: true, message: "作业种类不能为空", trigger: "change" }
        ],
        operItemsType: [
          { required: true, message: "作业项目不能为空", trigger: "change" }
        ],
        company: [{ required: true, message: "单位不能为空", trigger: "blur" }],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        payment: [
          { required: true, message: "缴费情况不能为空", trigger: "blur" }
        ],
        leader: [
          { min: 1, max: 15, message: "长度在 1 到 15 个字符", trigger: "blur" }
        ],
        leaderPhone: [
          { message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getDicts("busi_appl_edu").then(response => {
      this.eduTypeOptions = response.data;
    });
    this.getDicts("busi_oper_type").then(response => {
      this.operTypeOptions = response.data;
    });
    this.getDicts("busi_oper_items_type").then(response => {
      this.operItemsTypeOptions = response.data;
    });
    this.getDicts("pay_status").then(response => {
      this.paymentTypeOptions = response.data;
    });
  },
  methods: {
    checkPermi,
    checkRole,
    /** 查询报名管理列表 */
    getList() {
      this.loading = true;
      listAppl(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.applList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 学历字典翻译
    eduTypeFormat(row, column) {
      return this.selectDictLabel(this.eduTypeOptions, row.eduType);
    },
    // 作业种类字典翻译
    operTypeFormat(row, column) {
      return this.selectDictLabel(this.operTypeOptions, row.operType);
    },
    // 作业项目字典翻译
    operItemsTypeFormat(row, column) {
      return this.selectDictLabel(this.operItemsTypeOptions, row.operItemsType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        applId: null,
        applNm: null,
        sex: null,
        identity: null,
        applTime: null,
        eduType: null,
        operType: null,
        operItemsType: null,
        theoryScores: null,
        practiceScores: null,
        company: null,
        addr: null,
        phone: null,
        tel: null,
        payment: null,
        leader: null,
        leaderPhone: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.applId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报名管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const applId = row.applId || this.ids;
      getAppl(applId).then(response => {
        console.log("response.data", response.data);
        this.form = response.data;
        this.open = true;
        this.title = "修改报名管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form.applId);
          if (this.form.applId != null) {
            updateAppl(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppl(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const applIds = row.applId || this.ids;
      this.$confirm(
        '是否确认删除报名管理编号为"' + applIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delAppl(applIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有报名管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.exportLoading = true;
          return exportAppl(queryParams);
        })
        .then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "报名导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        console.log('response.msg',response.msg);
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
